package com.ming.pullloadmorerecyclerview;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ming.pullloadmorerecyclerview_lib.PullLoadMoreView;

import java.util.ArrayList;
import java.util.List;

/**
 * Author MingRuQi
 * E-mail mingruqi@sina.cn
 * DateTime 2019/2/18 11:31
 */
public class LinerActivity extends AppCompatActivity implements PullLoadMoreView.PullLoadListener,PullLoadMoreView.LoadMoreListener{

    private PullLoadMoreView pullLoadMoreView;
    private LinerAdapter linerAdapter;
    final List<Integer> contents = new ArrayList<>();
    int k = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liner);
        linerAdapter = new LinerAdapter(getBaseContext());
        pullLoadMoreView = findViewById(R.id.pull_liner);


        pullLoadMoreView
                .setInitRefreshAndMoreEnable(true, true)
                .setInitOnPullLoadListener(this)
                .setInitOnLoadMoreListener(this)
                .setInitFooterViewEnable(true)
                .setInitAdapter(linerAdapter)
                .commit();
        pullLoadMoreView.onRefresh();

    }

    @Override
    public void onRefresh() {
        contents.clear();
        k = 0;
        for (int i = 0; i <20; i++) {
            contents.add(i);
        }
        pullLoadMoreView.getSwipeRefreshLayout().setRefreshing(false);
        linerAdapter.setContents(contents);
    }

    @Override
    public void onLoadMore() {
        final List<Integer> contenta = new ArrayList<>();
        if (k < 2) {
            k++;
            for (int i = 0; i < 10; i++) {
                contenta.add(i);
            }
            //((SimpleItemAnimator) pullLoadMoreView.getRecyclerView().getItemAnimator()).setSupportsChangeAnimations(false);
            linerAdapter.addContents(contenta);
            // pullLoadMoreView.complete();
        }else {
            pullLoadMoreView.onNoMore();
        }
    }
}
